<?php
class Mlogin extends CI_Model{

    var $details;

    public function __construct(){
        parent::__construct();
        $this->load->database();
    }

    public function getUser(){
        $this->db->select(' id, username, password, type');
        $this->db->from('login');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function validate_user( $username, $password ) {
        $this->db->from('user');
        $this->db->where('username',$username );
        $this->db->where( 'password', sha1($password) );
        $login = $this->db->get()->result();

        // If a value exists, then the user account exists and is validated
        if ( is_array($login) && count($login) == 1 ) {
            // Set the users details into the $details property of this class
            $this->details = $login[0];
            // Call set_session to set the user's session vars via CodeIgniter
            $this->set_session();
            return true;
        }
        return false;
    }

    public function set_session() {

        $this->session->set_userdata( array(
                'id'=>$this->details->id,
                'name'=> $this->details->name,
                'username'=>$this->details->username,
                'password'=>$this->details->password,
                'isLoggedIn'=>true
            )
        );
    }
}
?>